package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.entity.Role;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.RoleService;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import java.util.Date;

/**
 * @author ：xiexing
 * @description：角色管理Controller
 * @date ：2020/8/24 21:53
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private UserUtils userUtils;

    /**
     * @description 角色管理
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/")
    public String rolePage(Model model) {
        return "page/system/role";
    }

    /**
     * @description 获取角色列表
     * @author XieXing
     * @date 2020/8/16 12:02
     */
    @RequestMapping("/getRoleList")
    @ResponseBody
    public ResResult getRoleList(Role role, Integer page, Integer limit) {
        ResResult resResult = roleService.listByPage(role, page, limit);
        resResult.setOptName("获取角色列表");
        return resResult;
    }

    /**
     * @description 获取全部角色信息
     * @author XieXing
     * @date 2020/10/12 14:15
     */
    @RequestMapping("/getRoles")
    @ResponseBody
    public ResResult getRoles() {
        return roleService.selectSelective(null);
    }


    /**
     * @description 保存角色信息
     * @author XieXing
     * @date 2020/8/23 21:35
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveSys(Role role) {
        User user = userUtils.getCurrentUser();
        role.setRoleAddby(user.getUserName());
        role.setRoleAddbyId(user.getId());
        role.setRoleAddTime(new Date());
        role.setRoleIsEnable(1);
        ResResult resResult = roleService.insert(role);
        resResult.setOptName("保存角色信息");
        return resResult;
    }


    /**
     * @description 更新角色信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateSys(Role role) {
        User user = userUtils.getCurrentUser();
        role.setRoleUpdateby(user.getUserName());
        role.setRoleUpdatebyId(user.getId());
        role.setRoleUpdateTime(new Date());
        ResResult resResult = roleService.updateByPrimaryKeySelective(role);//更新角色信息
        resResult.setOptName("更新角色信息");
        return resResult;
    }

    /**
     * @description 删除角色信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delMenu(@PathVariable(value = "id") Integer id) {
        ResResult resResult = roleService.deleteByPrimaryKey(id);//删除角色信息
        resResult.setOptName("删除角色信息");
        return resResult;
    }

    /**
     * @description 更新角色状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateDataStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        //TODO 从session获取当前操作用户信息
        ResResult resResult = roleService.updateDataStatus(id, status);
        resResult.setOptName("更新角色状态");
        return resResult;
    }
}
